const express=require("express");
const app=express();
const mysql=require("mysql");
var bodyParser = require("body-parser");

app.use(express.static('front'));

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

app.all('*', function (req, res, next) {
  /* res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "X-Requested-With");
  res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
  res.header("X-Powered-By", ' 3.2.1'); */
  res.header("Content-Type", "application/json;charset=utf-8");
  next();
});

// 配置数据库信息
var connection = mysql.createConnection({
  host: 'localhost',
  port: '3306',
  user: 'root',
  password: 'ydhabc',
  database: 'front_end'
});

// 连接
connection.connect();

app.get('/', (req, res) => {
    console.log("/ 接口测试成功");
    res.set('Content-Type', 'text/html');
    res.sendFile(__dirname + '/front/html/index.html');
  });

  app.get('/map', (req, res) => {
    console.log("/map 接口测试成功")
    res.set('Content-Type', 'text/html');
    res.sendFile(__dirname + '/front/html/map.html');
  });

  app.get('/prototype', (req, res) => {
    console.log("/prototype 接口测试成功");
    res.set('Content-Type', 'text/html');
    res.sendFile(__dirname + '/front/html/prototype.html');
  });

  app.get('/research', (req, res) => {
    console.log("/research 接口测试成功");
    res.set('Content-Type', 'text/html');
    res.sendFile(__dirname + '/front/html/research.html');
  });

  app.get('/role', (req, res) => {
    console.log("/role 接口测试成功");
    res.set('Content-Type', 'text/html');
    res.sendFile(__dirname + '/front/html/role.html');
  });

  app.get('/evaluation', (req, res) => {
    console.log("/evaluation 接口测试成功");
    res.set('Content-Type', 'text/html');
    res.sendFile(__dirname + '/front/html/evaluation.html');
  });

  var deleteData=(lastInsert)=>{
    var sql="DELETE FROM results WHERE id = ?";
    var sql1="ALTER TABLE results AUTO_INCREMENT = 1";
    connection.query(sql,lastInsert,(err,data)=>{
      if(err){
        console.log(err);
      }
      else{
        connection.query(sql1,(err,data)=>{
          if(err)
            console.log(err);
        })
      }
    })
  }

  app.post('/message',(req,res)=>{
    console.log("/message 接口被调用")
    var result = {
      "status": "0",
      "message": "success",
      "checkNull":"0"
    }
    var sql="insert into results(sex,age,work,judgement,value,judgement1,appraise1,appraise2,appraise3,appraise4,appraise5,appraise6,appraise7,appraise8) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    var message=[
                req.body.sex,
                req.body.age,
                req.body.work,
                req.body.judgement,
                req.body.value,
                req.body.judgement1,
                req.body.appraise1,
                req.body.appraise2,
                req.body.appraise3,
                req.body.appraise4,
                req.body.appraise5,
                req.body.appraise6,
                req.body.appraise7,
                req.body.appraise8];
    connection.query(sql,message,(err,data)=>{
        if(err){
          console.log(err);
        }
        else{
          result.data=data;
        }
    })
    var sql2 = `
    SELECT *
    FROM results
    WHERE id = ?
    and sex is null
    and age is null
    and work is null
    and judgement is null
    and value = '[]'
    and judgement1 is null
    and appraise1 is null
    and appraise2 is null
    and appraise3 is null
    and appraise4 is null
    and appraise5 is null
    and appraise6 is null
    and appraise7 is null
    and appraise8 is null;
`   ;
    var sql1="SELECT LAST_INSERT_ID()";
    connection.query(sql1,(err,data)=>{
      if(err){
        console.log(err);
      }
      else{
        var lastInsert=data[0]['LAST_INSERT_ID()'];
        connection.query(sql2,lastInsert,(err,data)=>{
          if(err){
            console.log(err);
          }
          else{
            if(data.length>0){
              deleteData(lastInsert);
              result.checkNull="1";
            }
            res.end(JSON.stringify(result));
          }
        })
      }
    })
    
  })
app.listen(8080,()=>{
    console.log("系统开始监听服务器，端口8080占用中");
    console.log("主页面访问地址：http://localhost:8080");
});